package com.dhy.policy;

import com.dhy.beans.ClassscheduleClasssplit;
import com.dhy.mapper.ClassscheduleClasssplitMapper;

import java.util.List;
import java.util.stream.Collectors;

//具体节次确定
public class SectionPolicy extends DecorateEmptyClassRoom
{
    public SectionPolicy(EmptyClassRoom emptyClassRoom, ClassscheduleClasssplitMapper classscheduleClasssplitMapper, String ele) {
        super(emptyClassRoom, classscheduleClasssplitMapper, ele);
    }

    @Override
    public List<ClassscheduleClasssplit> getEmptyClassRooms()
    {

        List<ClassscheduleClasssplit> courseDetails=null;
        if(emptyClassRoom!=null&&(courseDetails=emptyClassRoom.getEmptyClassRooms())!=null)
        {
            //根据上面返回的结果范围中，进行筛选
            return  filter(courseDetails);
        }
        courseDetails=getAllCourseDetails();
        return filter(courseDetails);
    }

    //根据范围进行筛选
    public List<ClassscheduleClasssplit> filter(List<ClassscheduleClasssplit> courseDetails)
    {
        //不需要进行过滤操作
        if(ele==null)
        {
            return courseDetails;
        }
        List<ClassscheduleClasssplit> filterCourses = courseDetails.stream().filter(course -> {
            String time = course.getTime();
            if(time==null)
                return true;
            String[] split = time.split(" ");
            //当前课程当前节次不上课，留下，否则排除
            for (String s : split) {
                if(s.equals(ele))
                {
                   return false;
                }
            }
            return true;
        }).collect(Collectors.toList());
        //返回过滤后的结果
        return filterCourses;
    }

    //数据库查询出所有课程详细信息
    public List<ClassscheduleClasssplit> getAllCourseDetails()
    {
        return classscheduleClasssplitMapper.selectList(null);
    }
}
